﻿/*
 * 完成数据库配置文件的操作 
 */ 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace HousingDistribution
{
    //数据库配置信息
    struct DBConfInfo
    {
        public string server;
        public string db;
        public int port;
        public string uid;
        public string pwd;
    }

    class DBConf
    {
        public static DBConf Instance()
        {
            if (instance == null)
                instance = new DBConf();

            return instance;
        }

        public DBConfInfo ReadData()
        {
            string filePath = DB_CONF_FILE_PATH;

            DBConfInfo info = new DBConfInfo();

            //默认值
            info.server = "localhost";
            info.db = "mydb";
            info.uid = "root";
            info.pwd = "";
            info.port = 3306;

            try
            {
                using (XmlReader rdr = XmlReader.Create(filePath))
                {
                    while (rdr.Read())
                    {
                        if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "DBAddress")
                        {
                            info.server = rdr.ReadElementString();
                        }
                        else if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "DBName")
                        {
                            info.db = rdr.ReadElementString();
                        }
                        else if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "username")
                        {
                            info.uid = rdr.ReadElementString();
                        }
                        else if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "password")
                        {
                            info.pwd = rdr.ReadElementString();
                        }
                        else if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "port")
                        {
                            info.port = int.Parse(rdr.ReadElementString());
                        }
                    }
                    rdr.Close();
                }

                return info;
            }
            catch (Exception e)
            {
                //发生异常
                System.Windows.Forms.MessageBox.Show(e.Message);
                return info;
            }
            finally
            {
                //创建一个默认文件
                Save(info);
            }
        }

        public void Save(DBConfInfo info)
        {
            string filePath = DB_CONF_FILE_PATH;
            
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Indent = true;
            setting.NewLineOnAttributes = true;

            using (XmlWriter writer = XmlWriter.Create(filePath, setting))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("DBConfiguration");

                writer.WriteElementString("DBAddress", info.server);
                writer.WriteElementString("DBName", info.db);
                writer.WriteElementString("username", info.uid);
                writer.WriteElementString("password", info.pwd);
                writer.WriteElementString("port", info.port.ToString());

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
        }

        private DBConf()
        {
        }

        private static DBConf instance = null;
        private string DB_CONF_FILE_PATH = ".\\DBConf.xml";
    }
}
